package io.adrainty.bolee.insurance.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.adrainty.bolee.framework.commons.basic.ResultDTO;
import io.adrainty.bolee.framework.commons.utils.ResultDTOUtil;
import io.adrainty.bolee.insurance.models.entity.CoefficientEntity;
import io.adrainty.bolee.insurance.service.ICoefficientService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>CoefficientController</p>
 *
 * @author AdRainty
 * @version V1.0.0
 * @description 保险系数管理
 * @since 2025/8/11 10:21:28
 */

@Tag(name = "保障系数管理")
@RestController
@RequestMapping("/coefficent")
public class CoefficientController {

    @Resource
    private ICoefficientService coefficientService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @Operation(summary = "分页获取保障系数")
    public ResultDTO<IPage<CoefficientEntity>> findCoefficientPage(@PathVariable("pageNum") Integer pageNum,
                                                                   @PathVariable("pageSize") Integer pageSize,
                                                                   @RequestBody CoefficientEntity searchDTO) {
        IPage<CoefficientEntity> pageResult = coefficientService.findCoefficientPage(pageNum, pageSize, searchDTO);
        return ResultDTOUtil.success(pageResult);
    }

    @PostMapping("/list")
    @Operation(summary = "获取保障系数列表")
    public ResultDTO<List<CoefficientEntity>> findCoefficientList(@RequestBody CoefficientEntity entity) {
        List<CoefficientEntity> list = coefficientService.findCoefficientList(entity);
        return ResultDTOUtil.success(list);
    }

    @PutMapping
    @Operation(summary = "创建保障系数")
    public ResultDTO<CoefficientEntity> createCoefficient(@RequestBody CoefficientEntity entity) {
        CoefficientEntity coefficientEntity = coefficientService.createCoefficient(entity);
        return ResultDTOUtil.success(coefficientEntity);
    }

    @PatchMapping
    @Operation(summary = "修改保障系数")
    public ResultDTO<Boolean> updateCoefficient(@RequestBody CoefficientEntity entity) {
        Boolean updateResult = coefficientService.updateCoefficient(entity);
        return ResultDTOUtil.success(updateResult);
    }

    @DeleteMapping
    @Operation(summary = "删除保障系数")
    public ResultDTO<Boolean> deleteCoefficient(@RequestBody CoefficientEntity entity) {
        Boolean deleteResult = coefficientService.deleteCoefficient(Arrays.stream(entity.getCheckedIds()).toList());
        return ResultDTOUtil.success(deleteResult);
    }

    @PostMapping("/coefficent-key/{coefficientKey}")
    @Operation(summary = "通过系数项key获取系数项")
    public ResultDTO<CoefficientEntity> findByCoefficientKey(@PathVariable("coefficientKey") String coefficientKey) {
        CoefficientEntity result = coefficientService.findByCoefficientKey(coefficientKey);
        return ResultDTOUtil.success(result);
    }

}
